點擊New Application
輸入名字,點擊create
點擊Bot(Add Bot)
勾選Administrator
點擊OAuth2,點選Bot,下方將出現一個網址,那是邀請碼,按下Copy後,複製到網址列
最後授權即可
回到剛才的Bot畫面
裡面有一個token,copy點下去
建立資料夾,並將資料夾拖曳進vscode,在裡面建立一個bot的python檔案,以及items的json檔案
先點擊json檔,並輸入
{
"token":"Your token"
}
pip install discord.py
# 引用包
from discord.ext import commands
import discord
bot = commands.Bot(command_prefix="!")
@bot.event
async def on_ready():
print("Bot in ready")
import json
with open('items.json', "r", encoding = "utf8") as file:
data = json.load(file)
bot.run(data['token'])
python bot.py
確認是否能啟動
本次的程式碼如下
from discord.ext import commands
import discord
import json
bot = commands.Bot(command_prefix="!")
with open('items.json', "r", encoding = "utf8") as file:
data = json.load(file)
@bot.event
async def on_ready():
print("Bot in ready")
bot.run(data['token'])